"""
批量扫描指定网段内的存活IP
网段: 192.168.0.0/24 (192.168.0.1~ 192.168.0.255)
考虑如何才能更快速地扫描出结果
前置模块: IPy, ping命令的使用
"""
import subprocess
import IPy
from threading import Thread


def is_ip(address):
    try:
        global ips
        ips = IPy.IP(address)
        return True
    except:
        print('您的输出有问题，请再次输入！')
        return False

ip_user = input("请输入要检查的网段：")
while (is_ip(ip_user) == False):
    ip_user=input("请再次输入：")

def Alive(ip):
    p = subprocess.Popen("ping -w 1 {0} \n".format(ip),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

    if p.returncode == 0:
        print(f"{ip}通,{ip}已经被使用过了\n")
    else:
        print(f"{ip}不通,{ip}还未被使用过\n")

def main():
    lst=[]
    for i in ips:
        t = Thread(target=Alive,args=(i,))
        lst.append(t)
        t.start()
    for t in lst:
        t.join()

if __name__ == '__main__':
    main()
